# Copyright (c) 2017 Presto Labs Pte. Ltd.
# Author: neo, jaewon

import datetime
import unittest

import pytz

import coin.base.datetime_util as datetime_util


class DatetimeUtilUnittest(unittest.TestCase):
  def test_nanoseconds_from_epoch(self):
    with self.assertRaises(TypeError):
      datetime_util.nanoseconds_from_epoch('')
    with self.assertRaises(TypeError):
      datetime_util.nanoseconds_from_epoch(None)
    with self.assertRaises(OverflowError):
      datetime_util.nanoseconds_from_epoch(datetime.datetime(4444, 10, 2, 3, 4, 5))
    with self.assertRaises(OverflowError):
      datetime_util.nanoseconds_from_epoch(datetime.datetime(1010, 10, 2, 3, 4, 5))

    tz = pytz.timezone('UTC')
    time = tz.localize(datetime.datetime(2010, 10, 2, 3, 4, 5))
    self.assertEqual(datetime_util.nanoseconds_from_epoch(time), 1285988645000000000)

    tz = pytz.timezone('America/New_York')
    time = tz.localize(datetime.datetime(2010, 10, 2, 3, 4, 5))
    self.assertEqual(datetime_util.nanoseconds_from_epoch(time), 1286003045000000000)

    tz = pytz.timezone('Asia/Singapore')
    time = tz.localize(datetime.datetime(2010, 10, 2, 3, 4, 5))
    self.assertEqual(datetime_util.nanoseconds_from_epoch(time), 1285959845000000000)

  def test_to_datetime(self):
    now = datetime.datetime.now()
    now_ts = datetime_util.nanoseconds_from_epoch(now)
    self.assertEqual(now, datetime_util.to_datetime(now_ts))


if __name__ == "__main__":
  unittest.main()
